package com.by.course.config.web.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * 根据固定字典决定是否拦截
 * </p>
 *
 * @author liujingeng
 * @since 2021/04/20
 */
@Slf4j
@Component
public class DictIntercept implements UriInterceptStrategy {

    private final Set<String> dict = new HashSet<>();

    private final Set<String> defaultDict = new HashSet<>();

    private String prefix = "/wy";

    private static final String PROFILE = "prod";


    @Autowired
    private Environment environment;

    public String getActiveProfile() {
        return environment.getProperty("spring.profiles.active");
    }

    @PostConstruct
    public void init() {
        defaultDict.add("/css");
        defaultDict.add("/images");

        // 获取当前的 spring profile active
        if (!PROFILE.equals(getActiveProfile())) {
            prefix = "/wyTest";
        }
        log.info("prefix:{}", prefix);
        dict.add(prefix + "/doc.html");
        dict.add(prefix + "/api-docs");
        dict.add(prefix + "/swagger");
        dict.add(prefix + "/v3/api-docs");
        dict.add(prefix + "/swagger-resources");
        dict.add(prefix + "/webjars");
        dict.add(prefix + "/images");
        dict.add(prefix + "/user/login");
        dict.add(prefix + "/error");
        dict.add(prefix + "/base/mpweixin/api/Auth/getOpenid");
        dict.add(prefix + "/base/mpweixin/api/Auth/getPhoneNumber");
        dict.add(prefix + "/base/mpweixin/api/Auth/wxPayResult");
        dict.add(prefix + "/base/mpweixin/api/Auth/wxPayReturnResult");
        dict.add(prefix + "/medical/bodyRegion/getAll");
        dict.add(prefix + "/medical/painArea/getPainArea");
        dict.add(prefix + "/medical/painSymptom/getPainSymptom");
        dict.add(prefix + "/medical/config");
        dict.add(prefix + "/user/getUser");
        dict.add(prefix + "/medical/neural/getAll");
        dict.add(prefix + "/medical/neural/addNeural");
        dict.add(prefix + "/medical/neural/delete");
        dict.add(prefix + "/medical/neural/getNeuralDetail");
        dict.add(prefix + "/medical/neural/searchNeural");
        dict.add(prefix + "/medical/neural/update");
    }

    @Override
    public boolean verify(String uri) {
        for (String str : defaultDict) {
            if (uri.contains(str)) {
                return true;
            }
        }
        for (String str : dict) {
            if (uri.contains(str)) {
                return true;
            }
        }
        return false;
    }
}
